using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Plugin.QQOfflineMsg;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace MicroMsg.Manager
{
	public class QConversationMgr
	{
		private const string TAG = "QConversationMgr";
		private EventWatcher qConversationModifyWatcher;
		private int addMsgCount;
		public QConversationMgr()
		{
			this.init();
			ChatMsgMgr.registerCleanMsg(new Action(QConversationMgr.QQOfflineMsgClearAll));
		}
		private void init()
		{
			EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_ADD, new EventHandlerDelegate(this.onMsgAdd));
			EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_MODIFY, new EventHandlerDelegate(this.onMsgModify));
			EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_DEL, new EventHandlerDelegate(this.onMsgDel));
			this.qConversationModifyWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onQConversationChange));
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_QCONVERSATION_ADD, this.qConversationModifyWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_QCONVERSATION_MODIFY, this.qConversationModifyWatcher);
		}
		private void uninit()
		{
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_ADD, new EventHandlerDelegate(this.onMsgAdd));
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_MODIFY, new EventHandlerDelegate(this.onMsgModify));
			EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_DEL, new EventHandlerDelegate(this.onMsgDel));
			if (this.qConversationModifyWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_QCONVERSATION_ADD, this.qConversationModifyWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_QCONVERSATION_MODIFY, this.qConversationModifyWatcher);
				this.qConversationModifyWatcher = null;
			}
		}
		public ObservableCollection<QConversation> getList()
		{
			List<QConversation> list = StorageMgr.qconv.getList();
			if (list == null)
			{
				return null;
			}
			return new ObservableCollection<QConversation>(list);
		}
		private List<ChatMsg> getQMsgList(BaseEventArgs evtArgs)
		{
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return null;
			}
			List<ChatMsg> list = null;
			using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					if (ContactHelper.isQContact(current.strTalker))
					{
						if (list == null)
						{
							list = new List<ChatMsg>();
						}
						list.Add(current);
					}
				}
			}
			return list;
		}
		private QConversation msgToConversation(ChatMsg chatMsg_0, QConversation conv = null)
		{
			if (conv == null)
			{
				conv = new QConversation();
			}
			conv.nMsgLocalID = chatMsg_0.nMsgLocalID;
			conv.strUsrName = chatMsg_0.strTalker;
			conv.nIsSend = chatMsg_0.nIsSender;
			conv.nTime = chatMsg_0.nCreateTime;
			conv.strMsgType = string.Concat(chatMsg_0.nMsgType);
			conv.strNickName = ContactHelper.getDisplayName(conv.strUsrName, null);
			conv.strContent = chatMsg_0.strMsg;
			return conv;
		}
		private void onMsgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<ChatMsg> qMsgList = this.getQMsgList(evtArgs);
			if (qMsgList == null)
			{
				return;
			}
			List<AddMsgInfo> list = this.CombinationMsg(qMsgList);
			if (list != null && list.get_Count() != 0)
			{
				Log.smethod_5("QConversationMgr", "onChatAddMsgChange start");
				using (List<AddMsgInfo>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						AddMsgInfo current = enumerator.get_Current();
						this.ChatAddMsg(current);
					}
				}
				Log.smethod_5("QConversationMgr", "onChatAddMsgChange end");
				return;
			}
		}
		private void onMsgModify(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<ChatMsg> qMsgList = this.getQMsgList(evtArgs);
			if (qMsgList == null)
			{
				return;
			}
			using (List<ChatMsg>.Enumerator enumerator = qMsgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					this.ChatUpdateMsg(current, false, 1);
				}
			}
		}
		private void onMsgDel(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<ChatMsg> qMsgList = this.getQMsgList(evtArgs);
			if (qMsgList == null)
			{
				return;
			}
			using (List<ChatMsg>.Enumerator enumerator = qMsgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					QConversation qConversation = StorageMgr.qconv.method_0(current.strTalker);
					if (qConversation != null && qConversation.nMsgLocalID == current.nMsgLocalID)
					{
						int msgNum = StorageMgr.chatMsg.getMsgNum(qConversation.strUsrName);
						if (msgNum <= 0)
						{
							qConversation.strContent = "";
							qConversation.nIsSend = 0;
							qConversation.strMsgType = 36.ToString();
							StorageMgr.qconv.modify(qConversation);
						}
						else
						{
							List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList(qConversation.strUsrName, msgNum - 1, 1);
							if (msgList != null && msgList.get_Count() > 0)
							{
								this.msgToConversation(msgList.get_Item(0), qConversation);
								StorageMgr.qconv.modify(qConversation);
							}
						}
					}
				}
			}
		}
		private void onQConversationChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.getListObject<QConversation>() == null)
			{
				return;
			}
			EventCenter.postEvent(EventConst.ON_QCONVERSATION_LIST_CHANGE, evtArgs.mObject, null);
			QConversationMgr.updateMainConversation();
		}
		private List<AddMsgInfo> CombinationMsg(List<ChatMsg> listArgs)
		{
			if (listArgs != null && listArgs.get_Count() != 0)
			{
				Log.smethod_5("QConversationMgr", "CombinationMsg,List count:" + listArgs.get_Count());
				List<AddMsgInfo> list = new List<AddMsgInfo>();
				Dictionary<string, int> dictionary = new Dictionary<string, int>();
				for (int i = 0; i < listArgs.get_Count(); i++)
				{
					ChatMsg chatMsg = listArgs.get_Item(i);
					if (chatMsg != null && !string.IsNullOrEmpty(chatMsg.strTalker))
					{
						if (dictionary.ContainsKey(chatMsg.strTalker))
						{
							int num = dictionary.get_Item(chatMsg.strTalker);
							if (chatMsg.nMsgLocalID > list.get_Item(num).chatMsg_0.nMsgLocalID)
							{
								list.get_Item(num).chatMsg_0 = chatMsg;
							}
							if (chatMsg.nIsSender == 1)
							{
								list.get_Item(num).msgCount = 0;
							}
							else
							{
								if (!chatMsg.IsSysInfo())
								{
									list.get_Item(num).msgCount++;
								}
								if (!chatMsg.IsSysInfo() && !PageManager.IsTalkingToUser(chatMsg.strTalker))
								{
									list.get_Item(num).msgCountWithoutTalker++;
								}
							}
						}
						else
						{
							dictionary.set_Item(chatMsg.strTalker, list.get_Count());
							AddMsgInfo addMsgInfo = new AddMsgInfo();
							addMsgInfo.chatMsg_0 = chatMsg;
							if (chatMsg.nIsSender == 1)
							{
								addMsgInfo.msgCount = 0;
							}
							else
							{
								if (!chatMsg.IsSysInfo())
								{
									if (!PageManager.IsTalkingToUser(chatMsg.strTalker))
									{
										addMsgInfo.msgCountWithoutTalker = 1;
									}
									addMsgInfo.msgCount = 1;
								}
								else
								{
									addMsgInfo.msgCount = 0;
								}
							}
							list.Add(addMsgInfo);
						}
					}
				}
				Log.smethod_5("QConversationMgr", "CombinationMsg,after combin List count:" + list.get_Count());
				return list;
			}
			return null;
		}
		private void ChatAddMsg(AddMsgInfo addInfo)
		{
			if (addInfo == null)
			{
				return;
			}
			if (addInfo.chatMsg_0 == null)
			{
				return;
			}
			if (!addInfo.chatMsg_0.IsSupport())
			{
				return;
			}
			if (addInfo.chatMsg_0.nIsSender != 1)
			{
				this.addMsgCount += addInfo.msgCount;
			}
			this.ChatUpdateMsg(addInfo.chatMsg_0, true, addInfo.msgCount);
		}
		private void ChatUpdateMsg(ChatMsg chatMsg_0, bool isAddMsg = false, int combinMsgCount = 1)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (!chatMsg_0.IsSupport())
			{
				return;
			}
			QConversation qConversation = StorageMgr.qconv.method_0(chatMsg_0.strTalker);
			if (qConversation == null)
			{
				qConversation = new QConversation();
				qConversation.nStatus = 1;
				QConversationMgr.UpdateConversationFromMsg(chatMsg_0, qConversation, isAddMsg);
				if (qConversation.nIsSend == 1)
				{
					qConversation.nUnReadCount = 0;
				}
				else
				{
					if (PageManager.IsTalkingToUser(qConversation.strUsrName))
					{
						qConversation.nUnReadCount = 0;
						this.SetMsgRead(qConversation, chatMsg_0);
					}
					else
					{
						qConversation.nUnReadCount = combinMsgCount;
						this.SetMsgUnRead(qConversation, chatMsg_0);
					}
				}
				Log.smethod_5("QConversationMgr", "StorageMgr.qconv.add start");
				StorageMgr.qconv.vmethod_0(qConversation);
				Log.smethod_5("QConversationMgr", "StorageMgr.qconv.add end");
				return;
			}
			if (!isAddMsg && qConversation.nMsgLocalID != chatMsg_0.nMsgLocalID)
			{
				return;
			}
			this.SetMsgRead(qConversation, chatMsg_0);
			QConversationMgr.UpdateConversationFromMsg(chatMsg_0, qConversation, isAddMsg);
			if (isAddMsg && chatMsg_0.nIsSender != 1)
			{
				if (PageManager.IsTalkingToUser(qConversation.strUsrName))
				{
					qConversation.nUnReadCount = 0;
					this.SetMsgRead(qConversation, chatMsg_0);
				}
				else
				{
					qConversation.nUnReadCount += combinMsgCount;
					this.SetMsgUnRead(qConversation, chatMsg_0);
				}
			}
			Log.smethod_5("QConversationMgr", "StorageMgr.qconv.update start");
			StorageMgr.qconv.update(qConversation);
			Log.smethod_5("QConversationMgr", "StorageMgr.qconv.update end");
		}
		public static void UpdateConversationFromMsg(ChatMsg chatMsg_0, QConversation conv, bool isNewMsg)
		{
			if (chatMsg_0 != null && conv != null)
			{
				conv.nMsgLocalID = chatMsg_0.nMsgLocalID;
				conv.strUsrName = chatMsg_0.strTalker;
				conv.nIsSend = chatMsg_0.nIsSender;
				if (isNewMsg)
				{
					conv.nTime = chatMsg_0.nCreateTime;
				}
				else
				{
					if (conv.nTime <= 0L)
					{
						conv.nTime = chatMsg_0.nCreateTime;
					}
				}
				conv.strMsgType = string.Concat(chatMsg_0.nMsgType);
				conv.strNickName = ContactHelper.getDisplayName(chatMsg_0.strTalker, null);
				conv.strContent = chatMsg_0.strMsg;
				if (conv.strContent != null && conv.strContent.get_Length() >= 200)
				{
					conv.strContent = conv.strContent.Substring(0, 200);
				}
				return;
			}
		}
		public static void updateMainConversation()
		{
			Conversation conversation = StorageMgr.converation.method_0("qmessage");
			if (conversation == null)
			{
				conversation = new Conversation();
				conversation.strUsrName = "qmessage";
			}
			List<QConversation> list = StorageMgr.qconv.getList();
			if (list != null && list.get_Count() != 0)
			{
				QConversation qConversation = new QConversation();
				for (int i = 0; i < list.get_Count(); i++)
				{
					if (list.get_Item(i).nTime > qConversation.nTime)
					{
						qConversation = list.get_Item(i);
					}
				}
				conversation.strContent = qConversation.strContent;
				conversation.strNickName = qConversation.strUsrName;
				conversation.strMsgType = qConversation.strMsgType;
				conversation.nIsSend = qConversation.nIsSend;
				conversation.nTime = qConversation.nTime;
				conversation.nStatus = qConversation.nStatus;
				conversation.nUnReadCount = QConversationMgr.getUnReadCountAll();
				StorageMgr.converation.update(conversation);
				return;
			}
			conversation.strContent = "";
			conversation.nUnReadCount = 0;
			conversation.nIsSend = 0;
			conversation.strMsgType = 36.ToString();
			StorageMgr.converation.modify(conversation);
		}
		public static int getUnReadCountAll()
		{
			List<QConversation> list = StorageMgr.qconv.getList();
			if (list != null && list.get_Count() > 0)
			{
				int num = 0;
				using (List<QConversation>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						QConversation current = enumerator.get_Current();
						num += current.nUnReadCount;
					}
				}
				return num;
			}
			return 0;
		}
		private void SetMsgRead(QConversation item, ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (item == null)
			{
				return;
			}
			item.nStatus = 1;
			if (chatMsg_0.IsVoice() && chatMsg_0.nIsSender == 0 && !ChatMsgMgr.getVoiceMsgIsRead(chatMsg_0))
			{
				item.nStatus = 0;
			}
		}
		public void SetMsgUnRead(QConversation item, ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (item == null)
			{
				return;
			}
			if (chatMsg_0.IsSysInfo())
			{
				item.nStatus = 1;
				return;
			}
			item.nStatus = 0;
		}
		public static string parseMsgContentToShow(QConversation conv, ref string preUserNickName)
		{
			preUserNickName = null;
			if (conv == null)
			{
				return "";
			}
			ChatMsg chatMsg = new ChatMsg();
			chatMsg.strTalker = conv.strUsrName;
			chatMsg.strMsg = conv.strContent;
			chatMsg.nIsSender = conv.nIsSend;
			try
			{
				chatMsg.nMsgType = int.Parse(conv.strMsgType);
			}
			catch
			{
				chatMsg.nMsgType = 1;
			}
			return QConversationMgr.parseQMsgContentToShow(chatMsg, ref preUserNickName);
		}
		private static string parseQMsgContentToShow(ChatMsg chatMsg_0, ref string preUserNickName)
		{
			preUserNickName = null;
			if (chatMsg_0 == null)
			{
				return "";
			}
			if (string.IsNullOrEmpty(chatMsg_0.strTalker))
			{
				return "";
			}
			if (chatMsg_0.strMsg == null)
			{
				chatMsg_0.strMsg = "";
			}
			string text = "";
			string text2 = "";
			preUserNickName = ContactHelper.getDisplayName(chatMsg_0.strTalker, null);
			int nMsgType = chatMsg_0.nMsgType;
			if (nMsgType <= 36)
			{
				if (nMsgType != 3)
				{
					switch (nMsgType)
					{
					case 34:
						text = strings.ChatVoiceContent;
						if (chatMsg_0.IsSender())
						{
							text = strings.Plugin_QQOffline_IReply + ": " + text;
						}
						break;
					case 36:
						if (chatMsg_0.IsSender())
						{
							text = strings.Plugin_QQOffline_IReply + ": " + chatMsg_0.strMsg;
						}
						else
						{
							if (string.IsNullOrEmpty(chatMsg_0.strMsg))
							{
								return "";
							}
							if (!ChatMsgMgr.getQQOfflineMsg(chatMsg_0.strMsg, ref text2, ref text))
							{
								text = "[" + strings.MessageContent_SendMessage_NotSupport + "]";
							}
						}
						break;
					}
				}
				else
				{
					text = strings.ChatImgContent;
				}
			}
			else
			{
				if (nMsgType != 39)
				{
					if (nMsgType == 47)
					{
						text = strings.ChatCustomSmileyContent;
					}
				}
				else
				{
					text = strings.Plugin_QQOffline_IReply + ": " + strings.ChatImgContent;
				}
			}
			return text;
		}
		public static bool delQConversation(string talker)
		{
			if (string.IsNullOrEmpty(talker))
			{
				return false;
			}
			ChatMsgMgr.deleteMsgByTalker(talker);
			return StorageMgr.qconv.method_5(talker);
		}
		public static void QQOfflineMsgClearAll()
		{
			List<QConversation> list = StorageMgr.qconv.getList();
			using (List<QConversation>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					QConversation current = enumerator.get_Current();
					QConversationMgr.delQConversation(current.strUsrName);
				}
			}
			ConversationMgr.delConversation("qmessage");
			string pluginPath = StorageIO.getPluginPath(QQOfflineMsg.getInstance().mName);
			StorageIO.emptyDir(pluginPath);
		}
	}
}
